package com.itheima.service.impl.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.impl.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @version 1.0
 * @Author 郭腾高
 * @Date 2024/9/4 20:31
 */
@Service
public class EmpServiceimpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    //查询员工
    public PageBean empList(Integer page, Integer pageSize, String name,
                            Short gender, LocalDate begin,
                            LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //执行查询
        List<Emp> empList = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) empList;
        //封装page对象
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }
    //批量删除员工
    @Override
    public void empDelete(List<Integer> ids) {
        empMapper.delete(ids);

    }
    //新增员工
    @Override
    public void insert(Emp emp) {
       emp.setCreateTime(LocalDateTime.now());
       emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp getById(Integer id) {
      Emp emp =  empMapper.selectById(id);
        return emp;
    }

    @Override
    public void updateById(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
       Emp e = empMapper.getByUsernameAndPassword(emp);
        return e;
    }
    //根据部门id删除对应部门员工信息
    @Override
    public void deleteByDeptId(Integer deptId) {
        empMapper.deleteById(deptId);
    }


}
